﻿using ApiTemplateGenerate.Extend;
using ApiTemplateGenerate.TypeConvert;
using System;
using System.Collections.Generic;
using System.Text;

namespace ApiTemplateGenerate
{
    /// <summary>
    /// 解析类型
    /// </summary>
    public static class AnalysisType
    {
        public static List<TsEnum> tsEnums = new List<TsEnum>();
        //处理通用泛型类型
        public static Dictionary<string, string> GenericsType = new Dictionary<string, string>
        {
            ["PageList"] = "PageList<{0}>",
            ["ModelEntity"] = "ModelEntity<{0}>",
            ["ListEntity"] = "ListEntity<{0}>",
            ["ResponseResult"] = "ResponseResult<{0}>",
            ["ResponsePage"] = "ResponsePage<{0}>",
        };
        public static Dictionary<string, string> GenericsTypeTS = new Dictionary<string, string>
        {
            ["ResponseResult"] = "ResponseResult<{0}>",
        };
        public static TypeInfo Start(Type typeInfo)
        {
            if (typeInfo.IsPrimitive || typeInfo.Name == "String")//是否是基元类型
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.BaseType };
            }
            if (typeInfo.IsClass && !typeInfo.IsPrimitive && !typeInfo.IsGenericType)
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.Class };
            }
            if (typeInfo.IsArray)
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.IsArray };
            }
            if (typeInfo.IsEnum)
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.Enum };
            }
            if (typeInfo.IsIEnumerableType())
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.IEnumerable };
            }
            if (typeInfo.IsGenericType && !typeInfo.IsNullableType())
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.Generic };
            }
            else
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.Other };
            }
        }

        public static bool IsT { get; set; } = false;

        public static string GetMethodReturn(Type type)
        {
            var stringName = "";
            //获取类型
            var info = Start(type);
            //分析类型
            switch (info.TypeEnum)
            {
                case TypeEnum.Class:
                    stringName = info.Type.Name;
                    break;
                case TypeEnum.BaseType:
                    stringName = TsType.TsTypeDic[info.Type.Name];
                    break;
                case TypeEnum.IEnumerable:
                case TypeEnum.IsArray:
                    stringName = "Array<{0}>";
                    var result = GetMethodReturn(type.GetGenericArguments()[0]);
                    stringName = string.Format(stringName, result);
                    break;
                case TypeEnum.Generic:
                    var sRetrun = info.Type.Name;
                    sRetrun = sRetrun.Replace("`1", "");
                    if (GenericsType.ContainsKey(sRetrun))//包含自定义的参数
                    {
                        IsT = true;
                        stringName = string.Format(GenericsType[sRetrun], GetMethodReturn(type.GetGenericArguments()[0]));
                        return stringName;
                    }
                    stringName = GetMethodReturn(type.GetGenericArguments()[0]);
                    return stringName;
                //break;
                case TypeEnum.Enum:
                    break;
                case TypeEnum.Other:
                    stringName = "any";
                    break;
                default:
                    break;
            }
            return stringName;
        }



        ///// <summary>
        ///// 获取路由
        ///// </summary>
        ///// <param name="type"></param>
        ///// <returns></returns>

        //public static string GetRoute(Type type)
        //{
        //    object[] objAttrs = type.GetCustomAttributes(typeof(RouteAttribute), true);
        //    if (objAttrs.Length > 0)
        //    {
        //        RouteAttribute attr = objAttrs[0] as RouteAttribute;
        //        if (attr != null)
        //        {
        //            return $"{attr.Template}";
        //        }
        //    }
        //    return string.Empty;
        //}
        //public static string GetRoute(MethodInfo type)
        //{
        //    object[] objAttrs = type.GetCustomAttributes(typeof(RouteAttribute), true);
        //    if (objAttrs.Length > 0)
        //    {
        //        RouteAttribute attr = objAttrs[0] as RouteAttribute;
        //        if (attr != null)
        //        {
        //            return $"{attr.Template}";
        //        }
        //    }
        //    return string.Empty;
        //}
    }

    public class TsEnumFiled
    {
        /// <summary>
        ///名称
        /// </summary>
        public string EnumName { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public object Value { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Describe { get; set; }
    }

    public class TsEnum
    {
        /// <summary>
        /// 描述
        /// </summary>
        public string Describe { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<TsEnumFiled> TsEnumFiled { get; set; } = new List<TsEnumFiled>();

    }





    public class TypeInfo
    {
        public TypeEnum TypeEnum { get; set; }
        public Type Type { get; set; }
    }

    public enum TypeEnum
    {
        //类
        Class = 1,
        //基本类型
        BaseType = 2,
        //IEnumerable List
        IEnumerable = 3,
        //arry
        IsArray = 4,
        //泛型
        Generic = 5,
        //enum
        Enum = 6,
        //其它类型
        Other = 7,
    }
}
